/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.Reportes.DAO;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pe.edu.pucp.Modelo.Actividad;
import pe.edu.pucp.Modelo.Log;
import pe.edu.pucp.Modelo.Objeto;
import pe.edu.pucp.Modelo.Perfil;
import pe.edu.pucp.Util.HibernateUtil;

/**
 *
 * @author jorge
 */
public class ReporteAuditoriaDAO {
    
    public ArrayList<Perfil> listarPerfil(){
        ArrayList<Perfil> listaPerfil = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
                    
        String q = "from Perfil";        
        Query query = session.createQuery(q);    
        List lista = query.list();
        Iterator iter = lista.iterator(); 
        while (iter.hasNext()) {    
            Perfil a = (Perfil) iter.next();
            listaPerfil.add(a);
        }
        
        tx.commit();
        session.close();

        return listaPerfil;
    }      
            
   
    public ArrayList<Objeto> listarObjeto(){
        ArrayList<Objeto> listaObjeto = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        
        String q = "from Objeto";                        
        Query query = session.createQuery(q);       
        List lista = query.list();
        Iterator iter = lista.iterator();        
        while (iter.hasNext()) {    
            Objeto a = (Objeto) iter.next();
            listaObjeto.add(a);
        }
        
        tx.commit();
        session.close();

        return listaObjeto;
    }       
    
    public ArrayList<Actividad> listarActividad(){
        ArrayList<Actividad> listaActividad = new ArrayList<>();
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();

        String q = "from Actividad";                        
        Query query = session.createQuery(q);                  
        List lista = query.list();        
        Iterator iter = lista.iterator(); 
        while (iter.hasNext()) {    
            Actividad a = (Actividad) iter.next();
            listaActividad.add(a);
        }
        
        tx.commit();
        session.close();

        return listaActividad;
    }

    public ArrayList<Log> listarLog(String nombre, int actv, int objt, Date inicio, Date fin){
        ArrayList<Log> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();

        String q = "select l from Log l where (upper(l.usuario.nombre) like '%'||:nomb||'%' "
                +"or upper(l.usuario.appaterno) like '%'||:nomb||'%') ";
        if (actv>0) { q += "and l.actividad.idactividad = :act ";  }
        if (objt>0) { q += "and l.objeto.idobjeto = :obj ";}
        if (inicio!=null){ q+="and cast(l.fechareg as date) >= cast(:dini as date) "; }
        if (fin!=null){ q+="and cast(l.fechareg as date) <= cast(:dfin as date) "; }          
        q += "order by l.idlog";   
        
        Query query = session.createQuery(q);                                
        query.setString("nomb", nombre);                
        if (actv>0) { query.setInteger("act", actv); }
        if (objt>0) { query.setInteger("obj", objt); }        
        if (inicio!=null){ query.setDate("dini", inicio); }
        if (fin!=null){ query.setDate("dfin", fin); }           
        
        List lista = query.list();
        Iterator iter = lista.iterator();
        while (iter.hasNext()) {    
            Log p = (Log) iter.next();
            Hibernate.initialize(p.getPerfil());
            Hibernate.initialize(p.getPerfil().getPermisos());
            Hibernate.initialize(p.getPerfil().getUsuarios());
            Hibernate.initialize(p.getPerfil().getEstadoPerfil());                    
            Hibernate.initialize(p.getUsuario());
            Hibernate.initialize(p.getUsuario().getNombre());
            Hibernate.initialize(p.getUsuario().getAppaterno());
            Hibernate.initialize(p.getObjeto());
            Hibernate.initialize(p.getObjeto());
            Hibernate.initialize(p.getActividad());            
            listaResultados.add(p);
        }        
        tx.commit();

        session.close();
        return listaResultados;
         
    }     
    
    public ArrayList<Log> listarLog(){
        ArrayList<Log> listaResultados = new ArrayList<>();       
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();

        List lista = session.createQuery("from Log order by idlog").list();        
        Iterator iter = lista.iterator();
        while (iter.hasNext()) {    
            Log p = (Log) iter.next();
            Hibernate.initialize(p.getPerfil());
            Hibernate.initialize(p.getPerfil().getPermisos());
            Hibernate.initialize(p.getPerfil().getUsuarios());
            Hibernate.initialize(p.getPerfil().getEstadoPerfil());                    
            Hibernate.initialize(p.getUsuario());
            Hibernate.initialize(p.getUsuario().getNombre());
            Hibernate.initialize(p.getUsuario().getAppaterno());
            Hibernate.initialize(p.getObjeto());
            Hibernate.initialize(p.getObjeto());
            Hibernate.initialize(p.getActividad());            
            listaResultados.add(p);
        }        
        tx.commit();

        session.close();
        return listaResultados;
         
    } 
       
}
